layui.use(['form', 'jquery', 'layer'], function () {
    var form = layui.form
        , layer = layui.layer
        , $ = layui.$;


    //自定义验证规则
    form.verify({
        userName: function (value) {
            if (value.length < 3) {
                return '用户名至少得3个字符啊';
            }
        }
        , pass: [
            /^[\S]{3,12}$/
            , '密码必须3到12位，且不能出现空格'
        ]
        , ip: [/(^$)|((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/,
            '请输入正确的ip地址'],
        phone1: [/(^$)|^1\d{10}$/, '请输入正确的手机号'],
        email1: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, '邮箱格式不正确'],
        url1: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, '链接格式不正确'],
        number1: [/(^$)|^\d+$/, '只能填写数字'],
        date1: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, '日期格式不正确'],
        identity1: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, '请输入正确的身份证号']
    });

    $.ajax({
        url: '/logisticsDriver/initialize',
        dataType: 'json',
        type: 'POST',
        async: false,
        success: function (resp) {
            $("#waybillId").val(resp.waybill);
            $("#waybillDeparture").val(resp.departure);
            $("#waybillBourn").val(resp.bourn);
            $("#carNum").val(resp.carNum);
            $("#carId").val(resp.carId);
            if (resp.state === 0) {
                $("#jd_btn").html("已接单");
                $("#jd_btn").attr("class", "layui-btn layui-btn-lg layui-btn-normal layui-btn-disabled");
                $("#sd_btn").attr("class", "layui-btn layui-btn-lg layui-btn-normal");
            } else {
                $("#jd_btn").on("click", function () {
                    if ($("#waybillId").val() === "暂无空闲运单" || $("#carNum").val() === "暂无空闲车辆") {
                            layer.msg("暂无空闲运单或车辆，稍后再试")
                    } else {
                        $.ajax({
                            url: '/logisticsDriver/driverWork',
                            dataType: 'json',
                            type: 'POST',
                            async: false,
                            data: {
                                waybillId: $('#waybillId').val(),
                                carId: $('#carId').val()
                            },
                            success: function (resp) {
                                if (resp.state === 1) {
                                    $("#jd_btn").html("运输中");
                                    $("#jd_btn").attr("class", "layui-btn layui-btn-lg layui-btn-normal layui-btn-disabled");
                                    $("#sd_btn").attr("class", "layui-btn layui-btn-lg layui-btn-normal");
                                }
                            },
                            error: function (response) {
                                console.log(response);
                            }
                        });
                    }
                })
            }
        },
        error: function (response) {
            console.log(response);
        }
    });

    //监听提交
    form.on('submit(sd)', function (data) {
        $.ajax({
            url: '/logisticsDriver/driverDelivery',
            dataType: 'json',
            type: 'POST',
            async: false,
            data: {
                waybillId: $('#waybillId').val(),
                carId: $('#carId').val()
            },
            success: function (resp) {
                if (resp.state === 1) {
                    $("#sd_btn").html("已送达");
                    $("#sd_btn").attr("class", "layui-btn layui-btn-lg layui-btn-normal layui-btn-disabled");
                }
            },
            error: function (response) {
                console.log(response);
            }
        });
        return false;
    });


})